package com.hdax.dmw.item.mapstruct;

import com.hdax.dmw.entity.base.Image;
import com.hdax.dmw.entity.item.Item;
import com.hdax.dmw.item.dtos.HotItemsDto;
import com.hdax.dmw.item.feign.BaseFeign;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Mapper(componentModel = "spring")
public abstract class HotItemDtoMapper {

    @Mappings({
            @Mapping(target = "imgUrl",expression = "java(imgUrl(source.getId()))")
    })
    public abstract HotItemsDto toTarget(Item source);
    public abstract List<HotItemsDto> toTargetList(List<Item> sources);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private BaseFeign baseFeign;

    public String imgUrl(Long itemId){
        //调用dmw-services-base服务的controller
        //http://localhost:7030/base/image
        //targetId=itemId type=2 category=1
//        String url = "http://localhost:7030/base/image?targetId="+itemId
//                + "&type=2&category=1";
//        ResponseEntity<Image> imageResponseEntity = restTemplate.postForEntity(url, null, Image.class);
//        Image image = imageResponseEntity.getBody();
//        return image == null ?
//                "":image.getImgUrl();
        Image image = baseFeign.image(itemId, 2, 1);
        return image == null ?
                "":image.getImgUrl();
    }
}
